﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GShare1._0.Client;
using GShare1._0.Server;

namespace GShare1._0
{
    public partial class Form2 : Form
    {
        private ClientConnection client;
        delegate void MboxInvoker(string msg);
        public Server.UIThreadInvoker UIThreadInvoker;
        MboxInvoker invoker = new MboxInvoker(MsgShow);

        private static void MsgShow(string msg)
        {
            MessageBox.Show(msg);
        }

        public Form2()
        {
            InitializeComponent();
            UIThreadInvoker = new Server.UIThreadInvoker(MainUIThreadWoker);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (client == null)
                {
                    button1.Text = "Connecting";
                    button1.Enabled = false;// Disable users input
                    //IPAddress ip = IPAddress.Loopback;
                    IPAddress ip = IPAddress.Parse(textBox1.Text);
                    int port = Int32.Parse(textBox2.Text);
                    
                    GUI_Client form = new GUI_Client();
                    client = new ClientConnection(ip, port,form.autoReset,textBox3.Text);
                    
                    form.Shown += form_Shown;
                    form.FormClosing += form_FormClosing;
                    form.tryConnect(client);
                    
                    
                    //If Fail Connect Reset Button and client for reconnection
                    if (client != null)
                    {
                        if (!client.Connected)
                        {
                            button1.Enabled = true;
                            button1.Text = "Connect";
                            client = null;
                        }
                    }
                    
                }

            }
            catch (Exception ex)
            {
                client = null;
                MessageBox.Show(ex.Message);

            }
        }

        void form_FormClosing(object sender, FormClosingEventArgs e)
        {
            GUI_Client obj = (GUI_Client)sender;
            obj.client.Disconnect();
            this.Show();
            button1.Enabled = true;
            button1.Text = "Connect";
            client = null;
        }

        void form_Shown(object sender, EventArgs e)
        {
            this.Hide();
        }

        void form_FormClosing1(object sender, FormClosingEventArgs e)
        {
            GUI_Server obj = (GUI_Server)sender;
            
            if (obj.server != null)
            {
                obj.server.Shutdown();
            }
            
            this.Show();

        }

        void form_Shown1(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void createServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GUI_Server form = new GUI_Server();
            form.Shown += form_Shown1;
            form.FormClosing += form_FormClosing1;

            form.ShowDialog(this);
            
        }

        private void MainUIThreadWoker(MethodInvoker MI)
        {
            this.BeginInvoke(MI);
        }


    }
}
